package cn.ecnu.user.controller;


import cn.ecnu.common.Permission;
import cn.ecnu.user.interceptor.Own;
import cn.ecnu.user.service.UserService;
import cn.ecnu.user.vo.AdminAllUser;
import cn.ecnu.user.vo.AdminUserVo;
import cn.ecnu.user.vo.ChangeRoleVo;
import cn.ecnu.user.vo.UserInfoVo;
import cn.ecnu.utils.R;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;


    @Own
    @GetMapping("/test")
    public R hello(HttpServletRequest request) {
        System.out.println(request.getHeader("userId"));
        System.out.println(request.getHeader("permission"));
        return R.ok().put("data", "hello");
    }

    @Own(permission = Permission.ADMIN)
    @GetMapping("/admin")
    public R admin() {
        return R.ok().put("data", "I'm admin");
    }


    @GetMapping("/all")
    public R getAllUsers() {
        AdminAllUser vo = userService.getAllUsers();
        return R.ok().put("data", vo);
    }


    // TODO: @Own(permission = Permission.ADMIN)
    @GetMapping("/delete/{uid}")
    public R deleteUserById(@PathVariable("uid") Integer uid) {
        userService.deleteUserById(uid);
        return R.ok();
    }


    @PostMapping("/update")
    public R updateUserById(@RequestBody UserInfoVo vo) {
        userService.updateUserById(vo);
        return R.ok();
    }



}
